/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver;

import java.io.*;
import com.phidgets.*;
import com.phidgets.event.*;
import javax.swing.JOptionPane;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
 *
 * @author Michael Ferry
 */
class DataCollectionTaskServer extends TimerTask {
    private BufferedWriter [] DataWriters = null;
    private String [] DataOutFiles;
    private InterfaceKitPhidget [] ifks;
    private String DatePattern=("MM/dd/yyyy' 'HH:mm:ss");
    private SimpleDateFormat DateFormatter = new SimpleDateFormat(DatePattern);
    
    
    //private int pHSensor=2;

    public DataCollectionTaskServer(String[] DataOutFiles, InterfaceKitPhidget[] ifks) {
        this.ifks = ifks;
        try {
            DataWriters = new BufferedWriter[DataOutFiles.length];
            // ifks = new IntefaceKitPhidget[DataOut]
            for (int i = 0; i < ifks.length; i++) {
                //System.out.println(i);
                //System.out.println(DataOutFiles[i]);
                DataWriters[i] = new BufferedWriter(new FileWriter(DataOutFiles[i]));
                //DataWriters[i].write(ifks[i].)
                DataWriters[i].write("Time");
                try {

                    if (ifks[i].isAttached()) {
                        for (int k = 0; k < ifks[i].getSensorCount(); k++) {
                            DataWriters[i].write("\t" + "S" + k);
                            DataWriters[i].write("\t" + "C" + k);
                            DataWriters[i].write("\t" + "R" + k);
                        }
                        for (int k = 0; k < ifks[i].getInputCount(); k++) {
                            DataWriters[i].write("\t" + "DI" + k);
                        }
                        for (int k = 0; k < ifks[i].getOutputCount(); k++) {
                            DataWriters[i].write("\t" + "DO" + k);
                        }
                    } else {
                        DataWriters[i].write("\t" + "Inteface kit not attached");
                    }
                    DataWriters[i].newLine();
                    DataWriters[i].flush();


                } catch (PhidgetException ex) {
                    System.out.println(ex);
                }
            }
//        }finally {
//            for (int i = 0; i < ifks.length; i++) {
//                if (DataWriters[i] != null) {
//                    DataWriters[i].close();
//                }
//
//            }
//
//        }j
        }catch (IOException e) {
            System.out.println(e);
        }
    }

    @Override
    public void run() {
        //String currTime = new Date(System.currentTimeMillis()).toString();
       // String currTime = new Date().toString();
        Date currTime = new Date();
        //String outString = currTime;

        System.out.println(DateFormatter.format(currTime));
        try {
            for (int j = 0; j < DataWriters.length; j++) {
                DataWriters[j].write(DateFormatter.format(currTime));

                try {
                    if (ifks[j].isAttached()) {
                        for (int k = 0; k < ifks[j].getSensorCount(); k++) {
                            DataWriters[j].write("\t" + ifks[j].getSensorRawValue(k));
                            DataWriters[j].write("\t" + SensorComputationsServer.ComputeSensorValue(ifks[j].getSerialNumber(), k, ifks[j].getSensorRawValue(k)));
                            DataWriters[j].write("\t" + ifks[j].getRatiometric());
                            //System.out.println(SensorComputations.ComputeSensorValue(174418, k, ifks[j].getSensorRawValue(k)));
                        }
                        for (int k = 0; k < ifks[j].getInputCount(); k++) {
                            DataWriters[j].write("\t" + ifks[j].getInputState(k));
                        }
                        for (int k = 0; k < ifks[j].getOutputCount(); k++) {
                            DataWriters[j].write("\t" + ifks[j].getOutputState(k));
                        }
                    } else {
                        DataWriters[j].write("\tNA");
                    }
                    
                } catch (PhidgetException ex) {
                    System.out.println(ex);
                    //JOptionPane.showMessageDialog(this, ex.getDescription(), "Phidget Error" + ex.getErrorNumber(), JOptionPane.ERROR_MESSAGE);
                    //dispose();
                    //System.exit(0);
                };
                DataWriters[j].newLine();
                DataWriters[j].flush();
                //System.out.println("flushed");
            }
        } catch (IOException e) {
            System.out.println(e);
        };
    }

    @Override
    public boolean cancel() {
        //System.out.println("in cancel function");
        try {
            for (int j = 0; j < DataWriters.length; j++) {
                DataWriters[j].close();
                //System.out.println("closing" + j);
            }
        } catch (IOException e) {
            System.out.println(e);
        }
        
        return true;
    }
}
